<template>
  <div>
    <!-- 顶部导航 -->
    <van-nav-bar class="page-nev-bar" title="登录">
      <van-icon class="left-icon" name="cross" slot="left" @click="$router.go(-1)" />
    </van-nav-bar>
    <!-- 表单 -->
    <van-form @submit="onSubmit" ref="loginFormRef">
      <van-field v-model="loginForm.mobile" name="mobile" placeholder="请输入手机号" :rules="loginFormRules.mobile"
        type="number" maxlength="11">
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field v-model="loginForm.code" name="code" placeholder="请输入验证码" left-icon="smile-o"
        :rules="loginFormRules.code" type="number" maxlength="6">
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <template #button>
          <!-- native-type="button"把表单内按钮变成普通按钮解决点击触发表单提交 -->
          <van-button v-if="showPostCode" class="send-sms-btn" size="small" type="primary" native-type="button"
            @click="postCode">发送验证码
          </van-button>
          <van-count-down v-else :time=" 60 * 1000 " @finish='showPostCode = true' format="ss s" class="send-sms-btn"
            size="small" />
        </template>
      </van-field>
      <div class="login-btn-wrap">
        <van-button block type="info" native-type="submit">
          登录
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, sendSms } from '@/api/user.js'
export default {
  name: 'Login',
  data () {
    return {
      // 倒计时显示
      showPostCode: true,
      // 表单数据
      loginForm: {
        mobile: '',
        code: ''
      },
      // 表单校验规则
      loginFormRules: {
        mobile: [
          { required: true, message: '请填写手机号' },
          { pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/, message: '手机号格式错误' }
        ],
        code: [
          { required: true, message: '请填写验证码' },
          { pattern: /^\d{6}$/, message: '验证码为6位数字' }
        ]
      }
    }
  },
  methods: {
    // 发送验证码
    async postCode () {
      // 校验手机号
      try {
        await this.$refs.loginFormRef.validate('mobile')
      } catch {
        return
      }
      this.$toast.loading({
        duration: 0, // 持续展示 toast
        message: '发送中...',
        forbidClick: true
      })
      // 请求
      try {
        await sendSms(this.loginForm.mobile)
        this.$toast.success('发送成功')
        this.showPostCode = false
      } catch (err) {
        if (err.response && err.response.status === 429) return this.$toast.fail('发送频繁')
        this.$toast.fail('系统异常')
      }
    },
    // 表单提交事件
    async onSubmit () {
      // $toast单列提示执行下一个$toast是这个就被覆盖
      this.$toast.loading({
        duration: 0, // 持续展示 toast
        message: '加载中...',
        forbidClick: true
      })
      // try中代码出错就进catch
      // 出错了不会影响下面代码执行
      try {
        const { data: res } = await login(this.loginForm)
        // 把数据传给vuex中的initUser
        this.$store.commit('initUser', res.data)
        this.$toast.success('登录成功')
        // 跳转到个人中心
        this.$router.push('/profile')
      } catch (err) {
        this.$toast.fail('登录失败')
      }
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { },
  mounted () { }
}
</script>

<style scoped lang='less'>
// 字体图标大小
.toutiao {
  font-size: 37px;
}
// 验证码按钮样式
.send-sms-btn {
  width: 152px;
  height: 46px;
  line-height: 46px;
  text-align: center;
  background-color: #ededed;
  font-size: 22px;
  color: #666;
  border: none;
  border-radius: 20px;
}
// 登录按钮样式
.login-btn-wrap {
  padding: 53px 33px;
  .van-button {
    background-color: #6db4fb;
    border-radius: 10px;
    border: none;
  }
}
</style>
